<template>
  <div>
        <div>
            <el-row class="">
              <el-col class="">
                <el-form ref="form"  label-position='right' size="small" :inline="true" label-width="110px">
                  <el-col :span="5" >
                      <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                          <el-input prefix-icon="el-icon-search" placeholder="请输入登录名" v-model="listQuery.condition.user_loginName.value" clearable></el-input>
                      </el-form-item>
                  </el-col>
                  <el-col :span="5" >
                      <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                          <el-input prefix-icon="el-icon-search" placeholder="请输入用户名" v-model="listQuery.condition.user_name.value" clearable></el-input>
                      </el-form-item>
                  </el-col>
                  <el-col :span="6" style="padding-left:5px;margin-top:12px;float:left;width:200px" algin="left">
                      <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="success" icon="el-icon-edit" @click="editTree('find')">查 询</el-button>
                      <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="danger" icon="el-icon-edit" @click="handleReset()">重 置</el-button>
                     </el-col>
                  </el-form>
                </el-col>

            </el-row>
            <el-table
              :key='tableKey'
              :data="userData"
              size="small"
              height="335px"
              highlight-current-row
              style="width: 100%;border:#eeeeee;">
            <el-table-column align="center" label="序号" min-width="50px" type="index">
              <template slot-scope="scope">
              <span>{{scope.$index + (serialNumber - 1) * listQuery.rowBounds.limit + 1}}</span>
              </template>
            </el-table-column>
            </el-table-column>
              <el-table-column
                prop="user_loginName"
                label="登录名"
                 align="center"
                width="180">
              </el-table-column>
              <el-table-column
                prop="user_name"
                label="用户名称"
                 align="center"
                width="180">
              </el-table-column>
              <el-table-column
                label="操作" align="center" >
                  <template slot-scope="scope">
                    <el-button @click="handleDelete(scope.row,'delete')" type="text" size="small">删除</el-button>
                  </template>
              </el-table-column>
            </el-table>
          <div class="pagination-container" style="margin-top: 1px;">
            <el-pagination background @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="listQuery.page" :page-sizes="[10,20,30, 50]"
                           :page-size="listQuery.rowBounds.limit" layout="total, sizes, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
          </div>

          <el-row :gutter="5">
            <el-col style="text-align: center; border-top:1px solid #e6e6e6; padding-top:20px; margin-top:10px;">
              <!--<el-button class="filter-item" type="primary" @click="saveData">保 存</el-button>-->
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-button size="small" class="filter-item" type="primary" @click="closeSelf"  style="float:right;">关 闭</el-button>
              <el-button size="small" class="filter-item" type="primary" icon="el-icon-circle-plus-outline" @click="handleCreateBatch" style="float:right; margin-right: 20px">添加秘书</el-button>
            </el-col>
          </el-row>
        </div>

      <el-dialog
        title="添加秘书"
        :visible.sync="addDialogVisible"
        append-to-body
        top="1vh"
        width="60%">
        <el-row class="">
          <el-col class="">
            <el-form ref="form"  label-position='right' size="small" :inline="true" label-width="110px">
              <el-col :span="5" >
                  <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                      <el-input prefix-icon="el-icon-search" placeholder="请输入登录名" v-model="listQueryAll.condition.user_loginName.value" clearable></el-input>
                  </el-form-item>
              </el-col>
              <el-col :span="5" >
                  <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                      <el-input prefix-icon="el-icon-search" placeholder="请输入用户名" v-model="listQueryAll.condition.user_name.value" clearable></el-input>
                  </el-form-item>
              </el-col>
              <el-col :span="6" style="padding-left:5px;margin-top:12px;float:left;width:200px" algin="left">
                <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="success" icon="el-icon-edit" @click="getList('find')">查 询</el-button>
                <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="danger" icon="el-icon-edit" @click="handleResetAll()">重 置</el-button>
               </el-col>
              </el-form>
            </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table
              :data="allUserData"
              size="small"
              height="355px"
              @select="selectionChange"
              @select-all="selectionChange"
              @selection-change="selectionValChange"
              style="width: 100%;overflow: auto;border:#eeeeee">
              <el-table-column
                type="selection"
                width="120" align="center" >
              </el-table-column>
              <el-table-column type="index" label="序号" width="50">
                <template slot-scope="scope">
                <span>{{scope.$index + (serialNumberAll - 1) * listQueryAll.rowBounds.limit + 1}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="user_loginName"
                label="登录名"
                width="200" align="center" >
              </el-table-column>
              <el-table-column
                prop="user_name"
                label="用户名称"
                 align="center" >
              </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination background @size-change="handleSizeChangeBatch"
                             @current-change="handleCurrentChangeBatch"
                             :page-sizes="[10,20,30, 50]"
                             layout="total, sizes, prev, pager, next, jumper"
                             :page-size="listQueryAll.rowBounds.limit"
                             :total="allTotal">
              </el-pagination>
            </div>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="insertList('insert')" type="primary">保存</el-button>
          <el-button size="small" @click="addDialogVisible = false" type="primary">关 闭</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import {
  secretarySelected,
  batchInsertItemSecretary,
  deleteHandle
} from '@/api/systemManage/userManagementEdit'
import {
  ptOptions,
  statusOptions,
  ptKeyValue
} from '@/utils/allOptions'
export default {
  name: 'editRoleUser',
  components: {},
  props: ['editSecretaryData'],
  data() {
    return {
      tableKey: 0,
      total: 0,
      treeData: [],
      loading: false,
      dialogVisible: false,
      addDialogVisible: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      oldMenuIdArr: [],
      listQuery: {
        rowBounds: {
          offset: 0,
          limit: 10
        },
        condition: {
          user_loginName: {
            property: 'user_loginName',
            value: '',
            type: 'LIKE'
          },
          user_name: {
            property: 'user_name',
            value: '',
            type: 'LIKE'
          }
        },
        fields: [
        ],
        sort: [{
          direction: 'ASC',
          property: 'user_orderNo'
        }]
      },
      listQueryAll: {
        rowBounds: {
          offset: 0,
          limit: 10
        },
        fields: [
        ],
        sort: [{
          direction: 'ASC',
          property: 'user_orderNo'
        }],
        condition: {
          user_name:{
            property: 'user_name',
            value: '',
            type: 'LIKE'
          },
          user_loginName:{
            property: 'user_loginName',
            value: '',
            type: 'LIKE'
          },
          user_id:{
            property: 'user_id',
            value: '',
            type: 'NE'
          }
        }
      },
      totalDemo: 0,
      form: '',
      userData: [],
      userDataDemo: [],
      allTotal: 0,
      allUserData: [],
      ptOptions,
      checkedUserIds: [],
      checkedUsers: [],
      checkedUserList: [],
      checkedOffset: 0,
      multipleSelection:[],
      serialNumber: 1,
      serialNumberAll: 1,
      statusOptions
    }
  },
  created() {
    this.editTree()
  },
  filters: {
    ptFilter(target) {
      return ptKeyValue[target]
    }
  },
  methods: {
    handleDelete(row,flag) {
    this.$confirm('此操作将删除人员【' + row.user_name + '】, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
      var rowDemo = [
        {
          secretaryId: row.user_id,
          managerId: this.editSecretaryData.userId
        }
      ]
      deleteHandle(rowDemo).then(response => {
        this.editTree(flag)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      })
     })
    },
    // 翻页器使用
    handleSizeChange(val) {
      this.listQuerypage = 1
      this.listQuery.rowBounds.limit = val
      this.listQuery.rowBounds.offset = this.listQuery.rowBounds.limit * (this.serialNumber - 1)
      this.editTree()
    },
    // 翻页器使用
    handleCurrentChange(val) {
      this.serialNumber = val
      this.listQuerypage = val
      this.listQuery.rowBounds.offset =
        this.listQuery.rowBounds.limit * (val - 1)
      this.editTree()
    },
    // 翻页器使用(添加秘书)
    handleSizeChangeBatch(val) {
      this.listQueryAll.rowBounds.limit = val
      this.listQueryAll.isExist = 0
      this.listQueryAll.rowBounds.offset = this.listQueryAll.rowBounds.limit * (this.serialNumberAll - 1)
      this.listQueryAll.managerId = this.editSecretaryData.userId
      this.listQueryAll.condition.user_id.value = this.editSecretaryData.userId
      secretarySelected(this.listQueryAll).then(response => {
        this.allUserData = response.data.data.records
        this.allTotal = response.data.data.count
      })
    },
    // 翻页器使用(添加秘书)
    handleCurrentChangeBatch(val) {
        this.serialNumberAll = val
        this.listQueryAll.rowBounds.offset =
        this.listQueryAll.rowBounds.limit * (val - 1)
        this.listQueryAll.isExist = 0
        this.listQueryAll.managerId = this.editSecretaryData.userId
        this.listQueryAll.condition.user_id.value = this.editSecretaryData.userId
        secretarySelected(this.listQueryAll).then(response => {
        this.allUserData = response.data.data.records
        this.allTotal = response.data.data.count
      })
    },
    handleCreateBatch() {
      this.addDialogVisible = true
      this.getList()
    },
    getList(flag) {
        if (flag ==='find') {
          this.checkedUsers=[];
          this.checkedUserIds=[];
          this.listQueryAll.rowBounds.offset = 0
          this.allTotal = 0
          this.serialNumberAll =1
        }
        this.listQueryAll.managerId = this.editSecretaryData.userId
        this.listQueryAll.condition.user_id.value = this.editSecretaryData.userId
        this.listQueryAll.isExist = 0

        if (flag ==='insert') {
          if ((this.listQueryAll.rowBounds.offset == (this.allTotal - 1)
          || this.allUserData.length == this.multipleSelection.length)
           && this.listQueryAll.rowBounds.offset >= this.listQueryAll.rowBounds.limit) {
            if (this.serialNumberAll * this.listQueryAll.rowBounds.limit >= this.allTotal) {
              this.serialNumberAll -=1
              this.listQueryAll.rowBounds.offset -=this.listQueryAll.rowBounds.limit
            }
          }
        }
        secretarySelected(this.listQueryAll).then(response => {
          this.allUserData = response.data.data.records
          this.allTotal = response.data.data.count
        })
    },
    editTree(flag) {
      this.loading = true
      this.listQuery.managerId = this.editSecretaryData.userId
      this.listQuery.isExist = 1
      console.log('this.listQuery')
      console.log(this.listQuery)
      if (flag ==='find') {
        this.listQuery.rowBounds.offset = 0
        this.total = 0
        this.serialNumber =1
      }
      if (flag === 'delete') {
        if (this.listQuery.rowBounds.offset == (this.total - 1)
         && this.listQuery.rowBounds.offset >= this.listQuery.rowBounds.limit) {
          if (this.serialNumber * this.listQuery.rowBounds.limit >= this.total) {
            this.listQuery.rowBounds.offset -=this.listQuery.rowBounds.limit
            this.serialNumber -=1
          }
        }
      }
      secretarySelected(this.listQuery).then(response => {
        this.userData = response.data.data.records
        this.total = response.data.data.count
        this.loading = false
      })
    },
    closeSelf() {
      this.$emit('changeCloseDialog')
    },
   selectionChange(val,row) {
     this.checkedUsers=[];
     this.checkedUserIds=[];
     console.log('this.editSecretaryData')
     console.log(this.editSecretaryData)
     if(val){
       for(var i in val){
           var tempUserManager = {
             managerId: '',
             secretaryId: ''
           }
          tempUserManager.managerId = this.editSecretaryData.userId
          tempUserManager.secretaryId = val[i].user_id
          this.checkedUsers.push(tempUserManager)
          this.checkedUserIds.push(val[i].user_id)
       }
     }
     this.checkedUserList = [];
     this.checkedUserList = val;
   },
   selectionValChange(val) {
    this.multipleSelection = val
   },
    insertList(flag) {
      if (this.checkedUsers.length > 0) {
        batchInsertItemSecretary(this.checkedUsers).then(response => {
          // 已选人员
          this.editTree()
          // 未选人员
          this.getList(flag)
          this.$message({
            message: '批量添加成功',
            type: 'success'
          })
        })
        this.checkedUsers = []
        this.checkedUserIds=[];
      } else {
          this.$message({
            message: '请选择人员',
            type: 'warning'
          })
      }
    },
     handleReset() {
       this.listQuery.condition.user_loginName.value = ''
       this.listQuery.condition.user_name.value = ''
       this.listQuery.rowBounds.offset = 0
       this.listQuery.rowBounds.limit = 10
       this.total = 0
       this.serialNumber = 1
       this.editTree()
     },
     handleResetAll() {
       this.checkedUsers=[];
       this.checkedUserIds=[];
       this.listQueryAll.condition.user_loginName.value = ''
       this.listQueryAll.condition.user_name.value = ''
       this.listQueryAll.condition.user_id.value = ''
       this.listQueryAll.rowBounds.offset = 0
       this.listQueryAll.rowBounds.limit = 10
       this.allTotal = 0
       this.serialNumberAll = 1
       this.getList()
     }
  }
}
</script>

<style scoped>
.treeArea {
    max-height: 66vh;
    overflow-y: auto;
}
</style>
